package com.snoob.lixing.dataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA
 * User: lixing
 * Date: 2018/11/25 22:58
 * <p>
 * 充当了DataSource的路由中介, 能有在运行时, 根据某种key值来动态切换到真正的DataSource上
 */
@Component
public class DynamicDataSource extends AbstractRoutingDataSource {

    /**
     * Determine the current lookup key. This will typically be
     * implemented to check a thread-bound transaction context.
     * <p>Allows for arbitrary keys. The returned key needs
     * to match the stored lookup key type, as resolved by the
     * {@link #resolveSpecifiedLookupKey} method.
     */
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDbSource();
    }
}
